package com.renjiyue.dao;


import com.renjiyue.IDTtil.IDutil;
import com.renjiyue.pojo.Blog;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import renjiyue.utils.Mybatisutils;

import java.util.*;

public class test01 {
    @Test
    public void addInitBlog(){
        SqlSession sqlSession = Mybatisutils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Blog blog = new Blog();
       // blog.setId(IDutil.genld());
        blog.setTitle("MyBatis如此简单");
        blog.setAuthor("任吉跃");
        blog.setCreateTime(new Date());
        blog.setViews(9999);
        mapper.addBlog(blog);

        sqlSession.close();
    }
    @Test
    public void testQueryBlogif(){
        SqlSession session = Mybatisutils.getSqlSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("title","MyBatis如此简单");
        map.put("author","任吉跃");
        List<Blog> blogs = mapper.queryBlogif(map);
        System.out.println("=========");
        System.out.println(blogs);
        session.close();
    }
    @Test
    public void testQueryBlogif2(){
        SqlSession session = Mybatisutils.getSqlSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("title","MyBatis如此简单");
        map.put("author","任吉跃");
        List<Blog> blogs = mapper.queryBlogif2(map);
        System.out.println("=========");
        System.out.println(blogs);
        session.close();
    }
   @Test
   public void testupdateBlog(){
       SqlSession session = Mybatisutils.getSqlSession();
       BlogMapper mapper = session.getMapper(BlogMapper.class);
       HashMap<String, String> map = new HashMap<String,String>();
       map.put("title","动态SQL");
       map.put("author","胡晓龙");
       map.put("id","shuabi1234");
       mapper.updateBlog(map);
       session.close();
   }
   @Test
    public void testqueryBlogchoose(){
       SqlSession session = Mybatisutils.getSqlSession();
       BlogMapper mapper = session.getMapper(BlogMapper.class);
       HashMap<String, Object> map = new HashMap<>();
       map.put("title","java如此简单");
       map.put("author","任吉跃");
       map.put("views",9999);
       for (Blog blog : mapper.queryBlogChoose(map)) {
           System.out.println(blog);
       }
       session.close();
   }

   @Test
    public void testQueryBlogForeach(){
       SqlSession session = Mybatisutils.getSqlSession();
       BlogMapper mapper = session.getMapper(BlogMapper.class);
       HashMap map = new HashMap();
       ArrayList<Integer> ids = new ArrayList<Integer>();
       ids.add(1);
       ids.add(2);
       ids.add(3);
       map.put("ids",ids);
       List<Blog> blogs = mapper.queryBlogForeach(map);
       System.out.println(blogs);
       session.close();
   }
}
